<script setup lang="ts">
  import { CollapseContainer } from '@/components/Container';
  import { CropperAvatar } from '@/components/Cropper';
  import { BasicForm, useForm } from '@/components/Form';
  import { Col, Row } from 'ant-design-vue';
  import { uploadApi } from '@/api/sys/upload';
  import { AccountItemType, baseDetailSchema } from './detail.data';
  import { onMounted, ref } from 'vue';
  import { queryById } from '@/api/system/account';

  const props = defineProps({ userId: { type: String, default: '' } });
  const account = ref<AccountItemType>({});
  const avatar = ref();
  const [register, { setFieldsValue }] = useForm({
    labelWidth: 120,
    schemas: baseDetailSchema,
    showActionButtonGroup: false,
  });
  onMounted(() => {
    account.value.userId = props.userId;
    queryDetail();
  });
  async function queryDetail() {
    account.value = await queryById(props.userId);
    setFieldsValue(account.value);
    avatar.value = account.value.avatar || '/src/assets/images/header.jpg';
  }
  function updateAvatar({ src, data }) {
    const userinfo = userStore.getUserInfo;
    userinfo.avatar = src;
    userStore.setUserInfo(userinfo);
    console.log('data', data);
  }
</script>

<template>
  <CollapseContainer title="基本信息" :canExpand="false">
    <Row :gutter="24">
      <Col :span="14">
        <BasicForm @register="register" />
      </Col>
      <Col :span="10">
        <div class="change-avatar">
          <div class="mb-2">头像</div>
          <CropperAvatar
            :uploadApi="uploadApi as any"
            :value="avatar"
            btnText="更换头像"
            :btnProps="{ preIcon: 'ant-design:cloud-upload-outlined' }"
            @change="updateAvatar"
            width="150"
          />
        </div>
      </Col>
    </Row>
  </CollapseContainer>
</template>

<style scoped lang="less">
  .change-avatar {
    img {
      display: block;
      margin-bottom: 15px;
      border-radius: 50%;
    }
  }
</style>
